package com.client;

import java.util.*;

import com.google.gwt.user.client.ui.InlineLabel;

public class PagedTagsPanel extends TagsPanel {
	private TagsBuff buff;

	public PagedTagsPanel(int pageSize, TagsBuff buff) {
		super(pageSize, 1, new ClkAddTag());
		this.buff = buff;
	}

	protected void onAttach() {
		this.insert(new InlineLabel(CNStatsFront.get().constants.relatedTags()), 0);
		super.onAttach();
	}

	public void showTags(String related, String direction) {
		CNStatsFront.get().plSearch.btnPrev.setEnabled(false);
		CNStatsFront.get().plSearch.btnNext.setEnabled(false);
		this.clearTags();
		CNStatsFront.get().showLoading(true);
		
		ArrayList<String> tags = this.buff.getTags(related, direction);
		if (null == tags) return;
		for (Iterator<String> iter = tags.iterator(); iter.hasNext();) {
			this.addTag(iter.next());
		}
		CNStatsFront.get().plSearch.btnPrev.setEnabled(this.buff.getCurrTags().hasPrev());
		CNStatsFront.get().plSearch.btnNext.setEnabled(this.buff.getCurrTags().hasNext());
		CNStatsFront.get().showLoading(false);
	}

	public void next() {
		this.showTags(this.buff.getCurr(), "next");
	}

	public void prev() {
		this.showTags(this.buff.getCurr(), "prev");
	}

	public void reset() {
		this.buff.reset();
		this.showTags("", "next");
	}
}
